package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 17. 电话号码的字母组合
 * https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/description/
 */
public class LetterCombinations {

    public List<String> letterCombinations(String digits){
        /*char[][] chars = {
                {'a', 'b', 'c'},
                {'d', 'e', 'f'},
                {'g', 'h', 'i'},
                {'j', 'k', 'l'},
                {'m', 'n', 'o'},
                {'p', 'q', 'r', 's'},
                {'t', 'u', 'v'},
                {'w', 'x', 'y', 'z'}
        };*/
        List<String> strs = new ArrayList<>();
        if(digits != null && !"".equals(digits)){
            add(digits, 0, new StringBuilder(), strs);
        }
        return strs;
    }



    private void add(String digits, int index, StringBuilder builder, List<String> list){
        if(index >= digits.length()){
            list.add(builder.toString());
            return;
        }
        char item = digits.charAt(index);
        if(item == 55){
            for(int i = 112; i < 116; i++){
                char thisChar = (char) i;
                StringBuilder builder1 = new StringBuilder(builder);
                builder1.append(thisChar);
                add(digits, index +1, builder1, list);
            }
        }else if(item == 56){
            for(int i = 116; i < 119; i++){
                char thisChar = (char) i;
                StringBuilder builder1 = new StringBuilder(builder);
                builder1.append(thisChar);
                add(digits, index +1, builder1, list);
            }
        }else if(item == 57){
            for(int i = 119; i < 123; i++){
                char thisChar = (char) i;
                StringBuilder builder1 = new StringBuilder(builder);
                builder1.append(thisChar);
                add(digits, index +1, builder1, list);
            }
        }else{
            int start = item + 47 + 2 * (item - 50);
            for(int i = start; i < start + 3; i++){
                char thisChar = (char) i;
                StringBuilder builder1 = new StringBuilder(builder);
                builder1.append(thisChar);
                add(digits, index +1, builder1, list);
            }
        }
    }

}
